﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;
namespace HotelATM_Guru
{
    public class CGetData
    {
        public static List<CPosATM> GetPosATMVietcomBank()
        {
            HtmlWeb hweb = new HtmlWeb();
            HtmlAgilityPack.HtmlDocument hdoc = hweb.Load("http://www.vcbhcm.com.vn/atm_dd.htm");

            HtmlNode parentNode = hdoc.DocumentNode.SelectSingleNode("//table[@id='edit_reg']");
            HtmlNode nextparentnode = parentNode.SelectSingleNode(".//table");
            HtmlNodeCollection collectionNode = nextparentnode.SelectNodes(".//tr[position()>2]");
            List<CPosATM> lstAtm = new List<CPosATM>();

            foreach (HtmlNode anode in collectionNode)
            {
                CPosATM atm = new CPosATM();
                if (anode.SelectNodes(".//td").Count == 4)
                {
                    HtmlNode node = anode.SelectSingleNode(".//td[3]");
                    HtmlNode inode = anode.SelectSingleNode(".//td[4]");
                    atm.place = node.InnerText.Trim();
                    atm.address = inode.InnerText.Trim();
                    atm.workinghour = "24/24";
                    atm.lat = "";
                    atm.lng = "";
                }
                else if (anode.SelectNodes(".//td").Count == 3)
                {
                    HtmlNode node = anode.SelectSingleNode(".//td[2]");
                    HtmlNode inode = anode.SelectSingleNode(".//td[3]");
                    atm.place = node.InnerText.Trim();
                    atm.address = inode.InnerText.Trim();
                    atm.workinghour = "24/24";
                    atm.lat = "";
                    atm.lng = "";
                }
                lstAtm.Add(atm);
            }
            return lstAtm;
        }

        public static List<CPosATM> GetPosATMACBBank()
        {
            var request = (HttpWebRequest)WebRequest.Create("http://www.acb.com.vn/mangluoi/?mlatm=VHCM&mlatm_dis=ALL");

            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            var respond = request.GetResponse();
            var respondStream = respond.GetResponseStream();
            var reader = new StreamReader(respondStream);
            var result = reader.ReadToEnd();
            Regex r = new Regex("<TR align=\"left\" valign=\"top\" class=row(a|b) ><td width=\"6%\" align='center'>(.*?)</td><td width=\"23%\">(.*?)</td><td width=\"55%\">(.*?)</td><td width=\"17%\" align='center'>(.*?)</td></tr>");

            MatchCollection ma = r.Matches(result);
            var atms = new List<CPosATM>();
            CPosATM temp;
            foreach (Match m in ma)
            {
                temp = new CPosATM();
                temp.place = System.Web.HttpUtility.HtmlDecode(m.Groups[3].ToString());
                temp.address = System.Web.HttpUtility.HtmlDecode(m.Groups[4].ToString());
                temp.workinghour = m.Groups[5].ToString();
                temp.lat = "";
                temp.lng = "";
                atms.Add(temp);
            }
            return atms;
        }

        public static List<CPosATM> GetPosATMDongABank()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.dongabank.com.vn/printer/atm/10/12/0");
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
            request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate";
            request.Headers[HttpRequestHeader.AcceptLanguage] = "Accept-Language: en-us,en;q=0.5";
            request.Headers[HttpRequestHeader.AcceptCharset] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            WebResponse respond = request.GetResponse();
            var respondStream = respond.GetResponseStream();
            var reader = new StreamReader(respondStream);
            var result = reader.ReadToEnd();

            Regex r = new Regex("<tr>\n<td width=\"34%\" height=\"31\">(.*?)</td>\n<td width=\"40%\">(.*?)</td>\n<td width=\"14%\" align=\"center\">\n(.*?)</td>\n<td width=\"12%\" align=\"center\">(.*?)</td>\n</tr>");
            MatchCollection ma = r.Matches(result);
            var atms = new List<CPosATM>();
            CPosATM temp;
            foreach (Match m in ma)
            {
                temp = new CPosATM();
                temp.place = System.Web.HttpUtility.HtmlDecode(m.Groups[1].ToString());
                temp.address = System.Web.HttpUtility.HtmlDecode(m.Groups[2].ToString());
                temp.workinghour = m.Groups[4].ToString();
                temp.lat = "";
                temp.lng = "";
                atms.Add(temp);
            }
            return atms;
        }

        public static List<CWeather> GetDataWeather()
        {
            List<CWeather> lstWeather = new List<CWeather>();
            CWeather wea = new CWeather();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.worldweatheronline.com/feed/weather.ashx?q=Ho+Chi+Minh+City,VietNam&format=xml&num_of_days=2&key=42c72f710c161843111601");
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
            request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate";
            request.Headers[HttpRequestHeader.AcceptLanguage] = "Accept-Language: en-us,en;q=0.5";
            request.Headers[HttpRequestHeader.AcceptCharset] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            WebResponse respond = request.GetResponse();
            var respondStream = respond.GetResponseStream();
            XmlDocument doc = new XmlDocument();
            doc.Load(respondStream);
            doc.CreateNavigator();

            XPathNavigator nav = doc.CreateNavigator();
            XPathNavigator navparent = nav.SelectSingleNode("//current_condition");
            wea.Date = "current";
            wea.temp_C = navparent.SelectSingleNode("./temp_C").InnerXml;
            wea.temp_F = navparent.SelectSingleNode("./temp_F").InnerXml;
            string weatherIconUrl = navparent.SelectSingleNode("./weatherIconUrl").InnerXml;
            wea.iconUrl = "weatherImg" + weatherIconUrl.Substring(weatherIconUrl.LastIndexOf("/"), weatherIconUrl.Length - weatherIconUrl.LastIndexOf("/"));//arrstr[4] + "/" + arrstr[5];
            wea.windspeedKmph = navparent.SelectSingleNode("./windspeedKmph").InnerXml;
            wea.humidity = navparent.SelectSingleNode("./humidity").InnerXml;
            wea.visibility = navparent.SelectSingleNode("./visibility").InnerXml;
            wea.pressure = navparent.SelectSingleNode("./pressure").InnerXml;
            wea.cloudcover = navparent.SelectSingleNode("./cloudcover").InnerXml;
            wea.weatherDesc = navparent.SelectSingleNode("./weatherDesc").InnerXml;
            XmlNodeList nodelist = doc.SelectNodes("//weather");
            lstWeather.Add(wea);
            CWeather temp;
            foreach (XmlNode anode in nodelist)
            {
                temp = new CWeather();
                temp.Date = anode.SelectSingleNode("./date").InnerText;
                temp.temp_C = anode.SelectSingleNode("./tempMinC").InnerText + "-" + anode.SelectSingleNode("./tempMaxC").InnerText;
                temp.temp_F = anode.SelectSingleNode("./tempMinF").InnerText + "-" + anode.SelectSingleNode("./tempMaxF").InnerText;
                temp.windspeedKmph = anode.SelectSingleNode("./windspeedKmph").InnerText;
                weatherIconUrl = anode.SelectSingleNode("./weatherIconUrl").InnerText;
                temp.iconUrl = "weatherImg" + weatherIconUrl.Substring(weatherIconUrl.LastIndexOf("/"), weatherIconUrl.Length - weatherIconUrl.LastIndexOf("/"));//arrstr[4] + "/" + arrstr[5];
                temp.weatherDesc = anode.SelectSingleNode("./weatherDesc").InnerText;
                lstWeather.Add(temp);
            }
            return lstWeather;
        }
    }
}
